<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('resizeImage'))
{

	function resizeImage($path,$width,$height,$dir_prefix="/thumbs_",$crop = false){
		$img_array = explode("/",$path);
		$name = $img_array[ count($img_array)-1 ];
		$dir = dirname($path).$dir_prefix;
		if( !is_dir($dir) ){
			mkdir( set_realpath($dir) , DIR_WRITE_MODE);
		}
		$newPath = $dir."/".$name;
		if(!read_file($newPath)){
			if( resize( $path,$width,$height,$newPath,$crop) ){
				return $newPath;
			}
		}
		return $newPath;
	} 
	/**
	 * resizes image based on parameters entered
	 * @param unknown_type $path
	 * @param unknown_type $width
	 * @param unknown_type $height
	 * @param unknown_type $newname
	 * @return boolean
	 */
	function resize( $path,$width,$height,$newname,$crop=false ){
		$controller = CI_Controller::get_instance();
		$controller->load->library('image_moo');
	    // single thumbnail
	    if(!$crop){
	    	$controller->image_moo
	        ->load($path)
	        ->resize($width,$height)
	        ->save($newname,TRUE);
	    }
	    else {
	    	$controller->image_moo
	        ->load($path)
	        ->resize_crop($width,$height)
	        ->save($newname,TRUE);
	    }
        if ($controller->image_moo->errors) print $controller->image_moo->display_errors();
	}
}
?>